<template>
  <el-dialog
    v-el-drag-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <!-- <el-form-item label="图片路径" prop="imgPath">
      <el-input v-model="dataForm.imgPath" placeholder="图片路径"></el-input>
    </el-form-item> -->
    <el-form-item label="标题" prop="title">
      <el-input style="width: 445px" v-model="dataForm.title" placeholder="标题"></el-input>
    </el-form-item>
    <el-form-item label="开始时间" prop="time">
      <template>
        <el-time-select
          placeholder="起始时间"
          v-model="startTime"
          :picker-options="{
          start: '00:00',
          step: '01:00',
          end: '24:00'
          }">
        </el-time-select>
        <el-time-select
          placeholder="结束时间"
          v-model="endTime"
          :picker-options="{
          start: '00:00',
          step: '01:00',
          end: '24:00',
          minTime: startTime
          }">
        </el-time-select>
      </template>
      <el-button type="primary" @click="addTime()">添加</el-button>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input type="textarea" v-model="dataForm.remark" style="width: 445px" placeholder="备注"></el-input>
    </el-form-item>
    <el-table v-if="this.dataForm.time.length !== 0"
      :data="this.dataForm.time"
      border
      style="width: 100%">
      <el-table-column
        prop="beginTime"
        header-align="center"
        align="center"
        label="开始时间">
      </el-table-column>
      <el-table-column
        prop="endTime"
        header-align="center"
        align="center"
        label="结束时间">
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small"  @click="deleteGoodsHandle(scope.$index)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        startTime: '',
        endTime: '',
        dataForm: {
          id: 0,
          title: '',
          remark: '',
          time: []
        },
        dataRule: {}
      }
    },
    methods: {
      init (id) {
        this.dataForm.time = []
        this.dataForm.remark = ''
        this.dataForm.title = ''
        this.startTime = ''
        this.endTime = ''
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
        //   this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/activity/spike/getActivityTemplateDetails/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.title = data.data.title
                this.dataForm.remark = data.data.remark
                this.dataForm.time = JSON.parse(data.data.timeInterval)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        if (this.dataForm.time.length <= 0) {
          this.$message.error('活动时间不能为空！')
          return
        }
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/activity/spike/${!this.dataForm.id ? 'addActivityTemplate' : 'modifyActivityTemplateById'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'timeInterval': JSON.stringify(this.dataForm.time),
                'title': this.dataForm.title,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      addTime () {
        if (this.startTime === '' || this.endTime === '') {
          this.$message.error('请选择开始或结束时间!')
          return
        }
        debugger
        if (this.startTime === '24:00') {
          this.startTime = '23:59'
        }
        if (this.endTime === '24:00') {
          this.endTime = '23:59'
        }
        this.dataForm.time.push({beginTime: this.startTime, endTime: this.endTime})
        console.log(JSON.stringify(this.dataForm.time))
        this.startTime = ''
        this.endTime = ''
      },
      deleteGoodsHandle (index) {
        this.dataForm.time.splice(index, 1)
      }
    }
  }
</script>
